package com.sinodata.bsm.center.engine.event;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.sinodata.bsm.center.bean.EventRuleBean;
import com.sinodata.bsm.common.vo.Event;

/**
 * 
 * <p>
 * Description: 事件规则组抽象类
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-24 PM 9:08:06     liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public abstract class EventRuleGroup {

    protected Set<EventRuleBean> rules = new HashSet<EventRuleBean>();

    protected Long eventTypeId;

    protected Long resId;

    /**
     * 规则状态，在EventRuleBean匹配后传入
     */
    // protected boolean ruleStatus = false;

    /**
     * 构建事件规则
     */
    public EventRuleGroup(Long resId, Long eventTypeId) {
        this.resId = resId;
        this.eventTypeId = eventTypeId;
    }

    /**
     * 向组内添加一个规则
     * @param rule
     */
    public void add(EventRuleBean eventRuleBean) {
        if (eventRuleBean.eventRule().getResId().longValue() == resId && eventRuleBean.eventRule().getEventTypeId().longValue() == eventTypeId && !rules.contains(eventRuleBean)) {
            rules.add(eventRuleBean);
        }
    }

    /**
     * 从组内删除一个规则
     * @param rule
     */
    public void remove(EventRuleBean rule) {
        if (rules.contains(rule)) {
            rules.remove(rule);
        }
    }

    /**
     * 从组内删除一个默认规则生成的规则,使用key值删除
     * 
     * add by liu 2012-11-27
     * @param rule
     */
    public void removeDefault(String key) {
        Iterator<EventRuleBean> it = rules.iterator();
        while (it.hasNext()) {
            EventRuleBean eventRuleBean = it.next();
            if (key.equals(eventRuleBean.eventRule().getRemarks())) {
                rules.remove(eventRuleBean);
                break;
            }
        }
    }

    /**
     * 匹配事件规则产生事件
     * @return
     */
    public abstract List<Event> matches(Long resId, Long propId);

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((eventTypeId == null) ? 0 : eventTypeId.hashCode());
        result = prime * result + ((resId == null) ? 0 : resId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        EventRuleGroup other = (EventRuleGroup) obj;
        if (eventTypeId == null) {
            if (other.eventTypeId != null)
                return false;
        } else if (!eventTypeId.equals(other.eventTypeId))
            return false;
        if (resId == null) {
            if (other.resId != null)
                return false;
        } else if (!resId.equals(other.resId))
            return false;
        return true;
    }

}
